package io.renren.modules.sys.service.impl;

import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import io.renren.common.utils.Pages;
import io.renren.modules.app.entity.UserEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.SysDictionaryDao;
import io.renren.modules.sys.entity.SysDictionaryEntity;
import io.renren.modules.sys.service.SysDictionaryService;
import org.apache.commons.lang.StringUtils;


@Service("sysDictionaryService")
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryDao, SysDictionaryEntity> implements SysDictionaryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDictionaryEntity> page = this.page(
                new Query<SysDictionaryEntity>().getPage(params),
                new QueryWrapper<SysDictionaryEntity>()
                .orderByAsc("type")
        );

        return new PageUtils(page);
    }

    @Override
    public Pages<SysDictionaryEntity> queryPage(SysDictionaryEntity sysDictionaryEntity) {
        String type = sysDictionaryEntity.getType();
        String label = sysDictionaryEntity.getLabel();
        String typeCn = sysDictionaryEntity.getTypeCn();

        IPage<SysDictionaryEntity> page = this.page(
                new Query<SysDictionaryEntity>().getPage(sysDictionaryEntity),
                new QueryWrapper<SysDictionaryEntity>()
                        .ne("deleteflag", "1")
                        .likeRight(StringUtils.isNotBlank(type), "type", type)
                        .likeRight(StringUtils.isNotBlank(label), "label", label)
                        .likeRight(StringUtils.isNotBlank(typeCn), "type_cn", typeCn)
                        .orderByAsc("type", "order_num")
        );

        return new Pages<SysDictionaryEntity>(page);
    }

    /**
     * 提供根据type/value/deleteflag 查询字典列表
     * 默认deleteflag为0
     * @param sysDictionary
     * @return
     */
    @Override
    public List<SysDictionaryEntity> loadDictionary(SysDictionaryEntity sysDictionary) {
        QueryWrapper<SysDictionaryEntity> queryWrapper = new QueryWrapper<SysDictionaryEntity>();
        if(!StringUtils.isEmpty(sysDictionary.getType())) {
            queryWrapper.eq("type", sysDictionary.getType());
        }
        if(!StringUtils.isEmpty(sysDictionary.getValue())) {
            queryWrapper.eq("value", sysDictionary.getValue());
        }
        String deleteflag = "0";
        if(!StringUtils.isEmpty(sysDictionary.getDeleteflag())) {
            deleteflag = sysDictionary.getDeleteflag();
        }
        queryWrapper.eq("deleteflag", deleteflag);
        queryWrapper.orderBy(true, true,"order_num", "value");
        return baseMapper.selectList(queryWrapper);
    }

}